package com.ruoyi.applet.service.impl;

import com.ruoyi.applet.domain.AppletSelect;
import com.ruoyi.applet.domain.AppletTeam;
import com.ruoyi.applet.domain.TopData;
import com.ruoyi.applet.mapper.AppletTeamMapper;
import com.ruoyi.applet.mapper.WechatUserMapper;
import com.ruoyi.applet.service.IAppletTeamService;
import com.brisk.common.core.domain.AjaxResult;
import com.brisk.common.utils.DateUtils;
import com.brisk.common.utils.SecurityUtils;
import com.brisk.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * appletTeamService业务层处理
 *
 * @author PengFeiDeng
 * @date 2022-05-30
 */
@Service
public class AppletTeamServiceImpl implements IAppletTeamService {
    @Autowired
    private AppletTeamMapper appletTeamMapper;

    @Autowired
    private WechatUserMapper wechatUserMapper;


    @Override
    public TopData getIndexTopData() {
        return appletTeamMapper.getIndexTopData();
    }

    /**
     * 查询appletTeam
     *
     * @param teamId appletTeam主键
     * @return appletTeam
     */
    @Override
    public AppletTeam selectAppletTeamByTeamId(Long teamId) {
        return appletTeamMapper.selectAppletTeamByTeamId(teamId);
    }

    /**
     * 查询appletTeam列表
     *
     * @param appletTeam appletTeam
     * @return appletTeam
     */
    @Override
    public List<AppletTeam> selectAppletTeamList(AppletTeam appletTeam) {
        List<AppletTeam> teamList = appletTeamMapper.selectAppletTeamList(appletTeam);
        for (AppletTeam team : teamList) {
            //查询队伍当中人 兑换天数最多的就是队伍的累计天数  因为一个人就对应一个队伍
            Integer maxDaySum = wechatUserMapper.getMaxDaySumByTeamId(team.getTeamId());
            team.setMaxDaySum(maxDaySum == null ? 0 : maxDaySum);
        }
        return teamList;
    }

    /**
     * 新增appletTeam
     *
     * @param appletTeam appletTeam
     * @return 结果
     */
    @Override
    public AjaxResult insertAppletTeam(AppletTeam appletTeam) {
        //重新是否存在
        AppletTeam select = new AppletTeam();
        select.setTeamName(appletTeam.getTeamName());
        List<AppletTeam> teamList = appletTeamMapper.selectAppletTeamList(select);
        if (teamList.size() > 0) return AjaxResult.error("队伍名称已存在！");
        appletTeam.setCreateTime(DateUtils.getNowDate());
        appletTeam.setCreateBy(SecurityUtils.getUsername());
        //如果没有上传LOGO就是默认的LOGO
        if (StringUtils.isEmpty(appletTeam.getTeamLogo()))
            appletTeam.setTeamLogo("https://briskwalking.oss-cn-chengdu.aliyuncs.com/2022/06/02/WechatIMG317_20220602140509A005.png");
        int res = appletTeamMapper.insertAppletTeam(appletTeam);
        return res > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改appletTeam
     *
     * @param appletTeam appletTeam
     * @return 结果
     */
    @Override
    public int updateAppletTeam(AppletTeam appletTeam) {
        appletTeam.setUpdateTime(DateUtils.getNowDate());
        appletTeam.setUpdateBy(SecurityUtils.getUsername());
        return appletTeamMapper.updateAppletTeam(appletTeam);
    }

    /**
     * 批量删除appletTeam
     *
     * @param teamIds 需要删除的appletTeam主键
     * @return 结果
     */
    @Override
    public int deleteAppletTeamByTeamIds(Long[] teamIds) {
        return appletTeamMapper.deleteAppletTeamByTeamIds(teamIds);
    }

    /**
     * 删除appletTeam信息
     *
     * @param teamId appletTeam主键
     * @return 结果
     */
    @Override
    public int deleteAppletTeamByTeamId(Long teamId) {
        return appletTeamMapper.deleteAppletTeamByTeamId(teamId);
    }

    @Override
    public List<AppletSelect> getTeamSelectList() {
        return appletTeamMapper.getTeamSelectList();
    }

    @Override
    public List<AppletTeam> getTeamRankingList(AppletTeam appletTeam) {
        List<AppletTeam> teamList = appletTeamMapper.selectAppletTeamListOrderByWalkSum(appletTeam);
        for (AppletTeam team : teamList) {
            //查询队伍当中人 兑换天数最多的就是队伍的累计天数  因为一个人就对应一个队伍
            Integer maxDaySum = wechatUserMapper.getMaxDaySumByTeamId(team.getTeamId());
            team.setMaxDaySum(maxDaySum == null ? 0 : maxDaySum);
        }
        return teamList;
    }

    @Override
    public int getTeamRankInList(Long teamId) {
        Integer rank = appletTeamMapper.getTeamRankInList(teamId);
        return rank == null ? 0 : rank;
    }


}
